<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('settings_images_form.class.php');
require_once('settings_images_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_images_dbc.class.php');


class Settings_Images_Form_Ctrl extends GForm_Ctrl_With_Object {
	protected function create_form() {
		$form = new Settings_Images_Form();

//		$form->generate_html();
		return $form;
	}


	protected function create_form_view() {
		$ret = new Settings_Images_Form_View($this->form);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		$bgcolor = $this->form->get_field_value('bgcolor');
		if (!ereg("^[0-9a-fA-F]{6}$", $bgcolor)) {
			$ret = false;
			$this->form->set_field_error('bgcolor', 'invalid_value');
		} else {
			$upload_path = $this->form->get_field_value('upload_path');
			$full_upload_path = $this->get_permanent_external_param('uploads_dir').$upload_path;
			if (file_exists($full_upload_path)) {
				if (!is_writable($full_upload_path)) {
					$ret = false;
					$this->export('path_current', $full_upload_path);
					$this->form->set_field_error('upload_path', 'invalid_path');
				}
			} else {
				$ret = false;
				$this->export('path_current', $full_upload_path);
				$this->form->set_field_error('upload_path', 'invalid_path');
			}
		}

		return $ret;
	}


	protected function on_bad_submit() {

//		printbr('lajna');
	}


	protected function create_object() {
		$object = new TCMS_Settings_Images_DBC();

		return $object;
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$form->set_field_value('allowed_extensions', $object->get_allowed_extensions());
		$form->set_field_value('to_size_x', $object->get_to_size_x());
		$form->set_field_value('to_size_y', $object->get_to_size_y());
		$form->set_field_value('jpg_quality', $object->get_jpg_quality());
		$form->set_field_value('bgcolor', $object->get_bgcolor());
		$form->set_field_value('keep_original', $object->get_keep_original());
		$form->set_field_value('thumbnail1_size_x', $object->get_thumbnail1_size_x());
		$form->set_field_value('thumbnail1_size_y', $object->get_thumbnail1_size_y());
		$form->set_field_value('thumbnail2_size_x', $object->get_thumbnail2_size_x());
		$form->set_field_value('thumbnail2_size_y', $object->get_thumbnail2_size_y());
		$form->set_field_value('maximum_size', $object->get_maximum_size());
		$form->set_field_value('maximum_size_x', $object->get_maximum_size_x());
		$form->set_field_value('maximum_size_y', $object->get_maximum_size_y());
		$form->set_field_value('minimum_ratio_xy', $object->get_minimum_ratio_xy());
		$form->set_field_value('maximum_ratio_xy', $object->get_maximum_ratio_xy());
		$form->set_field_value('watermark_image', $object->get_watermark_image());
		$form->set_field_value('watermark_position', $object->get_watermark_position());
		$form->set_field_value('upload_path', $object->get_upload_path());
	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$object->set_allowed_extensions($form->get_field_value('allowed_extensions'));
		$object->set_to_size_x($form->get_field_value('to_size_x'));
		$object->set_to_size_y($form->get_field_value('to_size_y'));
		$object->set_jpg_quality($form->get_field_value('jpg_quality'));
		$object->set_bgcolor($form->get_field_value('bgcolor'));
		$object->set_keep_original($form->get_field_value('keep_original'));
		$object->set_thumbnail1_size_x($form->get_field_value('thumbnail1_size_x'));
		$object->set_thumbnail1_size_y($form->get_field_value('thumbnail1_size_y'));
		$object->set_thumbnail2_size_x($form->get_field_value('thumbnail2_size_x'));
		$object->set_thumbnail2_size_y($form->get_field_value('thumbnail2_size_y'));
		$object->set_maximum_size($form->get_field_value('maximum_size'));
		$object->set_maximum_size_x($form->get_field_value('maximum_size_x'));
		$object->set_maximum_size_y($form->get_field_value('maximum_size_y'));
		$object->set_minimum_ratio_xy($form->get_field_value('minimum_ratio_xy'));
		$object->set_maximum_ratio_xy($form->get_field_value('maximum_ratio_xy'));
		$object->set_watermark_image($form->get_field_value('watermark_image'));
		$object->set_watermark_position($form->get_field_value('watermark_position'));
		$object->set_upload_path($form->get_field_value('upload_path'));
	}
}